clear;
% folder='C:\_bcoe\edf_data\';
% day='2003-11-03\';
% file='ian02.edf';


folder='C:\_bcoe\eyelink2_data\RK.2004_09_10\';
folder='C:\Data\data_eyelink2\RK.2004_09_10\';
file='ryo02.edf';

folder='C:\Data\data_eyelink2\2005-10-03.JPB\';
file='jpb02.edf';
cd(folder)

disp(' ');
fp_event=fopen(strcat(folder,file));
if(fp_event==-1)
   fprintf('    Can`t Open File: %s\n',[folder file])
   error('')
   return;
end
event=fread(fp_event,inf,'int16');
if isempty(event)
   disp('Event file is empty')
   return
end
max(event)
used_data=-40000; % use this to prevent duplicate useage of data
[val cnt]=int_hist(event(event~=0));
clf;bar(val,cnt)
cnt(val==-1)
[cnts order]=sort(cnt);
vals=val(order);
[cnts vals]
cnts((cnts>10000));
vals((cnts>10000));
temp=vals((cnts>10000))
magicnumber1=temp(find(temp<0,1,'last'));
magicnumber1=event==magicnumber1

event()


% [a b]=hist(event,min(event):max(event));

% c=a(a>0 & b~=0 & b~=used_data)';
% d=b(a>0 & b~=0 & b~=used_data)';
% [count1 order]=sort(c);
% value1=d(order);
% [value2 order]=sort(d);
% count2=c(order);
% clear a b c d
% [count1 value1 count2 value2 ];
% length(count1);


% -26880 % + 10 16bits o data
% -24169 % +  9 16bits o data
% -18688 % + 11 16bits o data
% -24137 % + 11 16bits o data


figure(3);clf;hold on;tickout;
set(gca,'position',[.02 .05 .97 .9])
set(gcf,'Position',[6 753 1275 201])

cla;
event2=event;

plot(event,'color',[.9 .9 .9])
h=plot(event,'.','color',[.7 .7 .7]);
axis tight
fred=[count1(length(count1)-20:end) value1(length(count1)-20:end) ]
disp('this is the 20 most frequent data points')

first=fred(fred(:,1)==max(max(fred(fred(:,2)<0,:))),2)
p_first=find(event2==first);
blue=p_first;
plot(p_first,event2(p_first),'ob');
temp1=p_first;
e_first=event2(p_first);
figure(11);clf;
set(gcf,'Position',[5 372 635 320])
for i = 1:10
 	subplot(11,1,i);axis tight;
 	plot(event2(p_first+i),'b');tickout;
	e_first(:,i+1)=event2(p_first+i);
	Xlim([1 length(p_first)]);
	temp1=[temp1; p_first+i];
end
figure(3);
%plot(temp1,event2(temp1),'.b')
event2((temp1))=used_data;
set(h,'ydata',event2)
[a b]=hist(event2,min(event2):max(event2));
c=a(a>0 & b~=0 & b~=used_data)';
d=b(a>0 & b~=0 & b~=used_data)';
[count order]=sort(c);
value=d(order);
fred=[count(length(count)-20:end) value(length(count)-20:end) ]
disp(['this is the 20 most frequent data points minus marker 1 (', num2str(first) , ')'])

second=fred(fred(:,1)==max(max(fred(fred(:,2)<0,:))),2)
p_second=find(event2==second);
red=p_second;
plot(p_second,event2(p_second),'or');
temp2=p_second;
e_second=event2(p_second);
figure(12);clf;
set(gcf,'Position',[648 372 635 320])
for i = 1:9
 	subplot(11,1,i);axis tight;
 	plot(event2(p_second+i),'r');tickout;
	e_second(:,i+1)=event2(p_second+i);
	Xlim([1 length(p_second)]);
	temp2=[temp2; p_second+i];
end
figure(3);
%plot(temp2,event2(temp2),'.r')
event2((temp2))=used_data;
set(h,'ydata',event2)
[a b]=hist(event2,min(event2):max(event2));
c=a(a>0 & b~=0 & b~=used_data)';
d=b(a>0 & b~=0 & b~=used_data)';
[count order]=sort(c);
value=d(order);
fred=[count(length(count)-20:end) value(length(count)-20:end) ]
disp(['this is the 20 most frequent data points minus markers 1&2 (',num2str(second) ')'])



p_18688=find(event2==-18688);
magenta=p_18688;
plot(p_18688,event2(p_18688),'om');
temp3=p_18688;
e_18688=event2(p_18688);
figure(13);clf;axis tight;
set(gcf,'Position',[648 36 635 320])
for i = 1:11
 	subplot(11,1,i)
 	plot(event2(p_18688+i),'m');tickout;
	e_18688(:,i+1)=event2(p_18688+i);
	Xlim([1 length(p_18688)]);
	temp3=[temp3; p_18688+i];
end
figure(3);
%plot(temp3,event2(temp3),'.m')
event2((temp3))=used_data;
set(h,'ydata',event2)
[a b]=hist(event2,min(event2):max(event2));
c=a(a>0 & b~=0 & b~=used_data)';
d=b(a>0 & b~=0 & b~=used_data)';
[count order]=sort(c);
value=d(order);
[count(length(count)-20:end) value(length(count)-20:end) ]
disp('this is the 20 most frequent data points minus markers 1&2&3 (-18688)')


p_24137=find(event2==-24137);
cyan=p_24137;
plot(p_24137,event2(p_24137),'oc');
temp4=p_24137;
e_24137=event2(p_24137);
figure(14);clf;
set(gcf,'Position',[5 36 635 320])
for i = 1:11
 	subplot(11,1,i)
 	plot(event2(p_24137+i),'c');tickout;
	e_24137(:,i+1)=event2(p_24137+i);
	Xlim([1 length(p_24137)]);
		temp4=[temp4; p_24137+i];
end
figure(3);
%plot(temp4,event2(temp4),'.c')
event2((temp4))=used_data;
set(h,'ydata',event2)
[a b]=hist(event2,min(event2):max(event2));
c=a(a>0 & b~=0 & b~=used_data)';
d=b(a>0 & b~=0 & b~=used_data)';
[count order]=sort(c);
value=d(order);
[count(length(count)-20:end) value(length(count)-20:end) ]
disp('this is the 20 most frequent data points minus markers 1&2&3&4 (-24137)')
figure(14)
figure(13)
figure(12)
figure(11)

percent_data_recovered=(1-(sum(event2~=used_data)/length(event2)))*100



fig(21);clf;tickout;hold on;set(gca,'position',[.02 .05 .97 .9])
iro='rgykm';
for i=1:5
plot(blue+i+1,(event(blue+i+1)/1000)+(100*i),'.' )
plot(cyan+i+2,(event(cyan+i+2)/1000)+(100*i),'.c')
plot(sort([blue+i+1;cyan+i+2]),(event(sort([blue+i+1;cyan+i+2]))/1000)+(100*i),iro(i))
end

fig(31);clf;tickout;hold on;set(gca,'position',[.02 .05 .97 .9])
iro='bgykcrgy';
for i=1:8
plot(red+i,(event(red+i)/100)+(100*i),'.r' )
plot(magenta+i+2,(event(magenta+i+2)/100)+(100*i),'.m')
plot(sort([red+i;magenta+i+2]),(event(sort([red+i;magenta+i+2]))/100)+(100*i),iro(i))
i=i+1;
end
fig(41);clf;tickout;hold on;set(gca,'position',[.03 .14 .96 .8])
plot(sort([red+1;magenta+3;]),event(sort([red+1;magenta+3])),'.r')
plot(sort([red+3;magenta+5;]),event(sort([red+3;magenta+5])),'.r')
plot(sort([red+2;magenta+4;]),event(sort([red+2;magenta+4])),'.b')
plot(sort([red+4;magenta+6;]),event(sort([red+4;magenta+6])),'.b')
axis tight;
ylim([1 20000])



return

    
plot(blue+2,event(blue+2),'.')
plot(cyan+3,event(cyan+3),'.c')
plot(sort([blue+2;cyan+3]),event(sort([blue+2;cyan+3])),'r')

plot(blue+3,event(blue+3),'.')
plot(cyan+4,event(cyan+4),'.c')
plot(sort([blue+3;cyan+4]),event(sort([blue+3;cyan+4])),'g')

plot(blue+4,event(blue+4),'.')
plot(cyan+5,event(cyan+5),'.c')
plot(sort([blue+4;cyan+5]),event(sort([blue+4;cyan+5])),'y')

plot(blue+5,event(blue+5),'.')
plot(cyan+6,event(cyan+6),'.c')
plot(sort([blue+5;cyan+6]),event(sort([blue+5;cyan+6])),'k')

plot(blue+8,event(blue+8),'.')
plot(cyan+9,event(cyan+9),'.c')
plot(sort([blue+8;cyan+9]),event(sort([blue+8;cyan+9])),'m')

fig(22);clf;hold on;set(gca,'position',[.02 .05 .97 .9])
plot(blue+6,event(blue+6),'.')
plot(cyan+7,event(cyan+7),'.c')
plot(sort([blue+6;cyan+7]),event(sort([blue+6;cyan+7])),'k')

plot(blue+7,event(blue+7),'.')
plot(cyan+8,event(cyan+8),'.c')
plot(sort([blue+7;cyan+8]),event(sort([blue+7;cyan+8])),'r')
%plot(sort([blue+7;cyan+8]),(x(1:2:end)*50)-45000,'.-')


fig(23);clf;hold on;set(gca,'position',[.03 .05 .96 .9])
iro=['k','b','g','y'];
for i=1:4
plot(red+i,event(red+i),'.r')
plot(magenta+i+2,event(magenta+i+2),'.m')
plot(sort([red+i;magenta+i+2]),event(sort([red+i;magenta+i+2])),iro(i))
end
plot(x(1:2:end))
plot(y(1:2:end),'r')

fig(24);clf;hold on;set(gca,'position',[.03 .05 .96 .9])
iro=['c','m','c','m','g','k','y','b','k']
for i=1:4
plot(event(sort([red+i;magenta+i+2])),iro(i))
end

fig(25);clf;hold on;set(gca,'position',[.03 .05 .96 .9])
iro=['c','m','c','m','g','k','y','b','k']
for i=5:9
plot(event(sort([red+i;magenta+i+2])),iro(i))
end
